from myelin.Ns import MYELINS

#from myelin.rdf.parsers.sparql.rdflib.serializers import AmaraSrx
from Ft.Rdf.Parsers.Sparql.Serializers import AmaraSrx

from nodetypes import config
from Commit import Form


class Form(Form):
    
    newResultsSerializer = staticmethod(AmaraSrx.NewSerializer)
    defaultNsMap = config.DEFAULT_NSMAP
    defaultGraphMethodMap = config.SERVICE_SPARQL_GRAPH_METHOD_DEFAULTS
    defaultPrettyMethods = config.DEFAULT_PRETTY_METHODS
    
    def createInstance(self, context, node):
        #eventData = context.event.data
        #map = context.uriMapper.apply
        
        #model = context.model
        #params = eventData.params
        #results = eventData.results
        
        nxa = node.xml_append
        nxc = node.xml_create_element
        
        super(Form,self).createInstance(
            context,
            nxa(
                nxc(
                    u'a:search-params', MYELINS
                )
            )
        )
        
        curNode = nxa(
            nxc(
                u'a:search-results', MYELINS,
            )
        )
        
        eventData = context.event.data
        imt = eventData.params.get(
            'graph-result-method'
        )
               
        curNode, imt = self.newResultsSerializer(
            eventData.results,
            imt and imt[0] or self.defaultGraphMethodMap.get(context.method)
        ).serialize(
            curNode,
            context.server.namespaceMap or self.defaultNsMap,
            context.preferredEncoding,
            context.method in self.defaultPrettyMethods
        )
        
        #curNode.xml_set_attribute(u'mime-type', unicode(imt.split(';')[0]))
        
        return node
    
del AmaraSrx